<template>

  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="账号"
          placeholder="请填写账号"
          :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="userPassword"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请输入密码' }]"
      />
    </van-cell-group>
    <van-divider>
      <van-highlight :keywords="keywordsOne" :source-string="textOne" id="keywordsLoginFontSize"/>
    </van-divider>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        登录
      </van-button>
    </div>
    <van-divider>
      <van-highlight :keywords="keywordsTwo"
                     :source-string="textTwo"
                     highlight-class="custom-class"
                     id="keywordsRegisterFontSize" @click="toUserRegisterPage"/>
    </van-divider>
  </van-form>
</template>


<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import myAxios from "../plugins/myAxios.ts";
import { ref } from "vue";
import {showSuccessToast, showToast} from "vant";
import {showFailToast} from "vant";

const route = useRoute();
const router = useRouter();

const userAccount = ref('');
const userPassword = ref('');
const textOne = '登录即享全部功能';
const keywordsOne = '登录即享全部功能';
const textTwo = '没有账号？点击注册';
const keywordsTwo = '没有账号？点击注册';

// 跳转用户注册页
const toUserRegisterPage = () => {
  router.push({
    path: '/user/register'
  })
}

const onSubmit = async () => {
  showToast('正在登录中 请稍候...')
  const res = await myAxios.post('/user/login',{
    userAccount: userAccount.value,
    userPassword: userPassword.value,
  })
  console.log(res, '用户登录');
  // @ts-ignore
  if(res.code === 0 && res.data){
    // 取出重定向信息 如果为空 默认重定向到【匹配伙伴首页】
    const redirectUrl = route.query?.redirectUrl ?? '/';
    // @ts-ignore
    window.location.href = redirectUrl;
    showSuccessToast('登录成功');
  } else {
    showFailToast(res.description ? `${res.description}` : '');
  }
};

</script>

<style scoped>
#keywordsLoginFontSize {
  font-size: 17px;
  opacity: 75%;
}
#keywordsRegisterFontSize {
  font-size: 15px;
  margin-top: 5px;
  opacity: 65%;
}
</style>